//
//  MYGIdCardTableViewCell.swift
//  MYDoctor
//
//  Created by chenjs on 2024/4/27.
//

import UIKit
import TZImagePickerController

class MYGIdCardTableViewCell: UITableViewCell {
    private var currentButton :UIButton?
    var model:MYGIDCardCellModel? {
        didSet{
            guard let item = model else {
                return
            }
//            idFrondButton.setImage(item.frontImage, for: .normal)
//            idBackButton.setImage(item.backImage, for: .normal)
        }
    }
    
    // MARK: - init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        contentView.backgroundColor = .white
        contentView.addSubview(tipLabel)
        contentView.addSubview(idFrondButton)
        contentView.addSubview(idBackButton)
        
        tipLabel.snp.makeConstraints { make in
            make.left.top.equalToSuperview().inset(25)
            make.height.equalTo(20)
        }
        
        idFrondButton.snp.makeConstraints { make in
            make.left.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(25)
            make.height.equalTo(96)
            make.width.equalTo((screenWidth - 73)/2)
        }
        
        idBackButton.snp.makeConstraints { make in
            make.right.equalToSuperview().inset(25)
            make.height.width.top.equalTo(idFrondButton)
        }
    }

    private lazy var tipLabel: UILabel = {
        let label = UILabel()
        label.textColor = .rgba(51, 51, 51, 1)
        label.font = .regular(16)
        label.text = "身份证照片"
        return label
    }()
    
    private lazy var idFrondButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "Register/frondid"), for: .normal)
        btn.addTarget(self, action: #selector(selectImage(_:)), for: .touchUpInside)
        return btn
    }()
    
    private lazy var idBackButton: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "Register/backid"), for: .normal)
        
        btn.addTarget(self, action: #selector(selectImage(_:)), for: .touchUpInside)
        return btn
    }()
}

extension MYGIdCardTableViewCell: TZImagePickerControllerDelegate {
        @objc
    private func selectImage(_ sender: UIButton) {
            self.currentButton = sender
            let pick = TZImagePickerController(maxImagesCount: 1, delegate: self)
            UIViewController.currentViewController?.present(pick!, animated: true)
        }
        
    func imagePickerController(_ picker: TZImagePickerController!, didFinishPickingPhotos photos: [UIImage]!, sourceAssets assets: [Any]!, isSelectOriginalPhoto: Bool, infos: [[AnyHashable : Any]]!) {
        guard let image = photos.first else {
            return
        }
        
        if self.currentButton == idFrondButton {
            model?.frontImage = image
            idFrondButton.setImage(image, for: .normal)
        }else {
            model?.backImage = image
            idBackButton.setImage(image, for: .normal)
        }

    }
}
